/* 
 * File:   TigerSimulator.cpp
 * Author: Hadrien
 * 
 * Created on 11 juillet 2012, 15:11
 */

#include "TigerSimulator.h"
#include <cstdlib>
#include <stdlib.h>

TigerSimulator::TigerSimulator() {
    reset();
}

TigerSimulator::~TigerSimulator() {
}

void TigerSimulator::reset() {
    m_state.setValue((TigerState::States)(rand() % state_t::size));
    terminated = false;
}

double TigerSimulator::control(const action_t& a) {
    if ((int)a.getValue()!=action_t::listen) {
        terminated = true;
    }
    return reward_function(m_state, a);
}

TigerSimulator::obs_t TigerSimulator::sense() {
    int r = rand() % 100;
    obs_t z;
    if(terminated) {
        z.setFinal(true);
        reset();
        if(r < 50)
            z.setValue(obs_t::hear_right);
        else
            z.setValue(obs_t::hear_left);
    }
    else {
        switch (m_state.getValue()) {
            case state_t::tiger_left:
                if(r < 85)
                    z.setValue(obs_t::hear_left);
                else
                    z.setValue(obs_t::hear_right);
                break;
            case state_t::tiger_right:
                if(r < 85)
                    z.setValue(obs_t::hear_right);
                else
                    z.setValue(obs_t::hear_left);
                break;
        }
    }
    return z;
}

